package com.lagou.edu.interceptor;

import com.mysql.jdbc.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

public class MyIntercepter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String name = request.getParameter("name");
        String pwd = request.getParameter("password");
        if(name != null && pwd != null){
            if(name.equals("admin") && pwd.equals("admin")){
                Cookie cookie = new Cookie("username","admin");
                cookie.setMaxAge(60*60*24);
                response.addCookie(cookie);
                request.getSession().setAttribute("username","admin");
            }else{
                request.setAttribute("msg","账户或密码错误");
                request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
                return false;
            }
        }else{
            if(request.getSession().getAttribute("username") == null){
                request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
                return false;
            }
        }

        return true;
    }
}
